<?php

class Core_Backend_Component_Customer_Controller_Shop extends Dcmp_Component_Controller {

//    public function indexAction($pageindex = 1, $pagesize = 20, $sort = 'id', $dir = 'asc') {
//        $this->getHistory()->record();
//        $validator = Kdf_Factory::findInstance('validation_validator')->setTarget($this->getRequest()->getParameter('filter'));
//        $collection = App::getModel('customer/collection_shop');
//        $dir == 'desc'? $collection->orderby()->desc($sort): $collection->orderby()->asc($sort);
//        if($validator->isNotEmpty('poi_id')){
//        	$collection->filterById($validator->getValue('poi_id'));
//        }
//        if($validator->isNotEmpty('shop_name')){
//        	$collection->filterByName($validator->getValue('shop_name'));
//        }
//        if($validator->isNotEmpty('road_name')){
//        	$collection->filterByName($validator->getValue('road_name'));
//        }
//        if($validator->isNotEmpty('industry_name')){
//        	$collection->filterByName($validator->getValue('industry_name'));
//        }
//        $collection->pagination($pageindex, $pagesize)->load();
//        $records = $collection->loadCount();
//        return $this->viewResult(array('list' => $collection, 'records' => $records));
//    }

    public function createAction($poiid) {
        $poi = App::getModel('foundation/poi')->load($poiid);
        return $this->viewResult(array('poi' => $poi));
    }

    public function editAction($poiid) {
        $model = App::getModel('customer/shop')->loadByPoi($poiid);
        if($model->getId()){
            return $this->viewResult(array('poi' => $model->getPoiInfo(), 'model' => $model));
        }
        return $this->redirectResult($this->getUrl(array('poiid'=>$poiid), 'create'));
    }

    public function applyPostAction() {
        return $this->saveModel('draft');
    }

    public function savePostAction() {
        return $this->saveModel('draft', TRUE);
    }

    public function submitPostAction() {
        return $this->saveModel('pending', TRUE);
    }

    public function publishPostAction($ids) {
        App::getModel('customer/shop')->publish($ids);
        return $this->redirectResult($this->getHistoryUrl('customer/index/index'));
    }

    public function forbidPostAction($ids, $statusmessage) {
        App::getModel('customer/shop')->forbid($ids, $statusmessage);
        return $this->redirectResult($this->getHistoryUrl('customer/index/index'));
    }

    private function saveModel($status, $return = FALSE) {
        $model = App::getModel('customer/shop');
        $data = $this->getRequest()->getForm('shop');
         if (isset($data['id']) && !empty($data['id'])) {
            $model->load($data['id']);
        }
        $model->addData($data)->setStatus($status);
        if (($errors = $model->validate() === TRUE)) {
            $model->save();
            $this->getMessager()->addInform($this->__f('share.message.saveSuccess', '{customer.label.shop_info}', $model->getPoiInfo()->getShopName()));
            return $return ?
                    $this->redirectResult($this->getHistoryUrl('customer/index/index')) :
                    $this->redirectResult($this->getUrl(array('poiid' => $model->getPoiId()), 'edit'));
        } else {
            $this->getMessager()->addError($this->__f('share.message.saveFail', '{customer.label.shop_info}', $model->getPoiInfo()->getShopName()))
                    ->addError($errors);
            return $this->viewResult(array('model' => $model), 'shop');
        }
    }

}